package com.xs.ai.services;

import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author hhl
 * @version 1.0
 * @description
 * @date 2025/8/5 0:04
 */
@Service
public class KnowledgeBaseService {

    private final VectorStore vectorStore;

    public KnowledgeBaseService(VectorStore vectorStore) {
        this.vectorStore = vectorStore;
    }

    public List<Document> searchRelevantDocuments(String query) {
        SearchRequest searchRequest = SearchRequest.query(query)
                .withTopK(3)
                .withSimilarityThreshold(0.2);

        return vectorStore.similaritySearch(searchRequest);
    }

    public void addDocumentToKnowledgeBase(String content) {
        Document document = new Document(content);
        vectorStore.add(Arrays.asList(document));
    }
}
